package com.leyikao.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
  
public class ClientAbortMethod {  
	/**
	 * 查询用户是否存在
	 * @param account账号
	 */
	public void queryUserInfoByAccount(String account){
		  
        // 创建默认的httpClient实例.    
        CloseableHttpClient httpclient = HttpClients.createDefault();  
        // 创建httppost    
        HttpPost httppost = new HttpPost("http://192.168.2.22:8080/leyikao/UserInfo.do?method=userValidate&account="+account);  
        
        try {
            CloseableHttpResponse response = httpclient.execute(httppost);  
            try {  
                HttpEntity entity = response.getEntity();  
                if (entity != null) {  
                    System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));  
                }  
            } finally {  
                response.close();  
            }  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (UnsupportedEncodingException e1) {  
            e1.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            // 关闭连接,释放资源    
            try {  
                httpclient.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    
	}
	/**
	 * 修改密码
	 * @param account 账号
	 * @param oldPwd 原密码
	 * @param newPwd 新密码
	 */
	public void modifyPassword(String account,String oldPwd,String newPwd){
		  
        // 创建默认的httpClient实例.    
        CloseableHttpClient httpclient = HttpClients.createDefault();  
        // 创建httppost    
        HttpPost httppost = new HttpPost("http://127.0.0.1:8080/leyikao/UserInfo.do?method=ModifyPwd&account="+account+"&currentPwd="+oldPwd+"&password="+newPwd);  
        
        try {
            CloseableHttpResponse response = httpclient.execute(httppost);  
            try {  
                HttpEntity entity = response.getEntity();  
                if (entity != null) {  
                    System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));  
                }  
            } finally {  
                response.close();  
            }  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (UnsupportedEncodingException e1) {  
            e1.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            // 关闭连接,释放资源    
            try {  
                httpclient.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    
	}
    public final static void main(String[] args){
    	ClientAbortMethod method = new ClientAbortMethod();
    	//验证用户是否存在，如果存在返回用户信息
    	method.queryUserInfoByAccount("123");
    	//修改用户密码
    	method.modifyPassword("123", "123", "123");
    }
  
}  
